package com.dykj.fireFighting.web.controller;

import java.util.*;

import com.dykj.fireFighting.api.base.file.service.OfficeService;
import com.dykj.fireFighting.api.monitoringCenter.entity.PgRegionRecord;
import com.dykj.fireFighting.api.monitoringCenter.entity.PgRegionRecordScore;
import com.dykj.fireFighting.api.monitoringCenter.service.IPgRegionRecordResultService;
import com.dykj.fireFighting.api.monitoringCenter.service.IPgRegionRecordScoreService;
import com.dykj.fireFighting.api.monitoringCenter.service.IPgRegionRecordService;
import com.dykj.fireFighting.api.socialUnit.dutymanger.entity.DutyRecord;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import com.dykj.fireFighting.web.utils.ExportUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.jxls.common.Context;
import org.jxls.transform.poi.PoiTransformer;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;



/**
 * 接口
 *
 * @Author JacksonTu
 * @Date  2022-02-21 14:49:22
 */
@Slf4j
@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping("pgRegionRecordScore")
public class PgRegionRecordScoreController {

    @DubboReference
    private IPgRegionRecordScoreService pgRegionRecordScoreService;
    @DubboReference
    private IPgRegionRecordService pgRegionRecordService;
    @DubboReference(check = false)
    private OfficeService officeService;

    /**
     * 列表
     *
     * @return
     */
    @AutoLog(value = "-分页列表查询")
    @ApiOperation(value = "分页列表", notes = "分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<PgRegionRecordScore>> treeGrid(CommonDto commonDto) {
        PageVo<PgRegionRecordScore> page = pgRegionRecordScoreService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取信息
     */
    @AutoLog(value = "-获取信息")
    @ApiOperation(value = "获取信息", notes = "获取信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<PgRegionRecordScore> info(@PathVariable("id") String id){
        PgRegionRecordScore pgRegionRecordScore = pgRegionRecordScoreService.getById(id);
        return CommonResult.success(pgRegionRecordScore);
    }

    /**
     * 保存信息
     */
    @AutoLog(value = "-保存信息")
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody PgRegionRecordScore pgRegionRecordScore) {
        try {
           pgRegionRecordScore.setCreateBy(ShiroUtil.getUser().getName());
            pgRegionRecordScore.setCreateTime(new Date());
                pgRegionRecordScoreService.save(pgRegionRecordScore);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }



    /**
     * 修改信息
     */
    @AutoLog(value = "-修改信息")
    @ApiOperation(value = "修改信息", notes = "修改信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody PgRegionRecordScore pgRegionRecordScore) {
        try {

                pgRegionRecordScoreService.updateById(pgRegionRecordScore);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除信息
     */
    @AutoLog(value = "-删除信息")
    @ApiOperation(value = "删除信息", notes = "删除信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                pgRegionRecordScoreService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     *
     * @param id
     * @param type 1三级指标评价结果汇总  2二级指标评价结果汇总 3风险因素排序
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("/downLoad")
    public void downLoad(@RequestParam("id") String id, @RequestParam("type") String type,HttpServletResponse response) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("regionId",id);
        PgRegionRecord regionRecord = pgRegionRecordService.selectByRegionId(map);
        map.put("recordId",regionRecord.getId());
        String modelName="";
        String xlsName="";
        if("1".equals(type)){
            map.put("level",1);
            modelName="三级指标评价结果汇总.xlsx";
            xlsName="sjzbpjhz.xlsx";
        }
        if("2".equals(type)){
            map.put("level",0);
            modelName="二级指标评价结果汇总.xlsx";
            xlsName="ejzbpjhz.xlsx";
        }
        if("3".equals(type)){
            map.put("orderby","orderby");
            modelName="风险因素排序.xlsx";
            xlsName="fxyspx.xlsx";
        }
        List<PgRegionRecordScore> regionRecordScores = pgRegionRecordScoreService.selectList(map);
        Context initialContext = PoiTransformer.createInitialContext();
        initialContext.putVar("regionRecordScores", regionRecordScores);
        ExportUtils.exportExcel(initialContext, xlsName, modelName, response);
    }


    /**
     *
     * @param id
     * @param type 1三级指标评价结果汇总  2二级指标评价结果汇总 3风险因素排序
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("/getRegion")
    public CommonResult<List<PgRegionRecordScore>> getRegion(@RequestParam("id") String id, @RequestParam("type") String type,HttpServletResponse response) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("regionId",id);
        PgRegionRecord regionRecord = pgRegionRecordService.selectByRegionId(map);
        map.put("recordId",regionRecord.getId());
        if("1".equals(type)){
            map.put("level",1);
        }
        if("2".equals(type)){
            map.put("level",0);
        }
        if("3".equals(type)){
            map.put("orderby","orderby");
        }
        List<PgRegionRecordScore> regionRecordScores = pgRegionRecordScoreService.selectList(map);
        return CommonResult.success(regionRecordScores);
    }
}
